/* Copyright (C) 2008, 2009, 2010 Faindu GmbH, Switzerland.
* All Rights Reserved. The following is Source Code and is subject to all restrictions 
* on such code as contained in the license accompanying this project. */
package blocstac.editimage.crop
{
import blocstac.editimage.base.ImageModel;
import blocstac.editimage.base.ImageOperation;

import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.Rectangle;

public class CropOperation implements ImageOperation
{
	private var _mask:Rectangle
	private var _imageOffset:Matrix
	
	private var _oldImageData:BitmapData
	private var _oldAngle:Number
	private var _oldEditedScaleX:Number
	private var _oldEditedScaleY:Number
	private var _oldImageDataUpdateIndex:int
	
	public function CropOperation(mask:Rectangle, imageOffset:Matrix)
	{
		_mask = mask
		_imageOffset = imageOffset
	}
	
	public function apply(state:ImageModel):void
	{
		_oldImageDataUpdateIndex = state.imageDataUpdateIndex
		_oldImageData = state.imageData
		_oldAngle = state.angle
		_oldEditedScaleX = state.editedScaleX
		_oldEditedScaleY = state.editedScaleY
		
		var newImageData:BitmapData = new BitmapData(_mask.width, _mask.height)
		newImageData.draw(state.imageData, _imageOffset, null, null, null, true)
		state.updateImageData(newImageData)
		
		state.angle = 0
		state.editedScaleX = 1
		state.editedScaleY = 1
	}
	
	public function undo(state:ImageModel):void
	{
		state.updateImageData(_oldImageData, _oldImageDataUpdateIndex)
		state.angle = _oldAngle
		state.editedScaleX = _oldEditedScaleX
		state.editedScaleY = _oldEditedScaleY
		_oldImageData = null
	}

}
}